package com.example.ruiji.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.Dto.SetmealDto;
import com.example.ruiji.common.Result;
import com.example.ruiji.entity.Category;
import com.example.ruiji.entity.Setmeal;
import com.example.ruiji.entity.SetmealDish;
import com.example.ruiji.mapper.CategoryMapper;
import com.example.ruiji.mapper.SetmealDishMapper;
import com.example.ruiji.mapper.SetmealMapper;
import com.example.ruiji.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public Result<String> save(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        });
        return Result.success("添加成功");
    }

    @Override
    public Result<Page<SetmealDto>> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(!StrUtil.isEmpty(name), Setmeal::getName, name);
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> setmealPage = setmealMapper.selectPage(pageInfo, setmealLambdaQueryWrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(setmealPage, setmealDtoPage);
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoRecords = new ArrayList<>();
        records.forEach(record -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Category category = categoryMapper.selectById(record.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            dtoRecords.add(setmealDto);
        });
        setmealDtoPage.setRecords(dtoRecords);
        return Result.success(setmealDtoPage);
    }

    @Override
    public Result<String> delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        Integer count = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (count > 0) {
            return Result.error("套餐正在售卖中，不能删除");
        }
        setmealMapper.deleteBatchIds(ids);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(setmealDishLambdaQueryWrapper);
        return Result.success("删除成功");
    }

    @Override
    public Result<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealMapper.selectList(setmealLambdaQueryWrapper);
        return Result.success(setmeals);
    }


}
